## Replication code for Estimating Candidates’ Political Orientation in a Polarized Congress
## We use R for 3.2.4 GUI 1.67 Mavericks build (7152), running on Mac OS 10.12.1 

library(foreign) ## We use version: foreign_0.8-66
library(ggplot2) ## We use version: ggplot2_2.1.0
library(plyr)    ## We use version: plyr_1.8.4
library(dplyr)   ## We use version: dplyr_0.5.0
library(doBy)    ## We use version: doBy_4.5-14
library(xtable)  ## We use version: xtable_1.8-2


load(file=paste("candidatepositions_house_analysis.RData",sep=""))


####################
### Correlation beweeen measures
####################

bonica_2000s<-subset(analysis_all, election>1998)
bonica_2000s$Party<-as.numeric(as.vector(bonica_2000s$Party))
grouped <- dplyr::group_by(bonica_2000s,ICPSR2)
bonica_2000s_grouped<-dplyr::summarise(grouped, 
dwnom1_contemporaneous2=mean(dwnom1_contemporaneous, na.rm=T),
party2=mean(Party, na.rm = T),
shor_mccarty_score=mean(shor_mccarty_score, na.rm = T),
npat_score2=mean(npat_score, na.rm = T),
twitter_idealpoint2=mean(twitter_idealpoint, na.rm = T),
am_estimate2=mean(am_estimate, na.rm = T),
cfscore2=mean(cfscore, na.rm = T),
js_ideology_contemporaneous2=mean(js_ideology_contemporaneous, na.rm = T)
)
bonica_2000s_grouped<-subset(bonica_2000s_grouped, party2==100 | party2==200)

#write.dta(bonica_2000s_grouped, "aggregated_data.dta")

cors_1 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(shor_mccarty_score, npat_score2, use="complete.obs"), 2))

cors_2 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(shor_mccarty_score, twitter_idealpoint2, use="complete.obs"), 2))

cors_3 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(shor_mccarty_score, cfscore2, use="complete.obs"), 2))

cors_4 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(shor_mccarty_score, am_estimate2, use="complete.obs"), 2))

cors_5 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(shor_mccarty_score, js_ideology_contemporaneous2, use="complete.obs"), 2))

cors_6 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(npat_score2, cfscore2, use="complete.obs"), 2))

cors_7 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(npat_score2, twitter_idealpoint2, use="complete.obs"), 2))

cors_8 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(npat_score2, am_estimate2, use="complete.obs"), 2))

cors_9 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(npat_score2, js_ideology_contemporaneous2, use="complete.obs"), 2))

cors_10 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(twitter_idealpoint2, cfscore2, use="complete.obs"), 2))

cors_11 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(twitter_idealpoint2, am_estimate2, use="complete.obs"), 2))

cors_12 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(twitter_idealpoint2, js_ideology_contemporaneous2, use="complete.obs"), 2))

cors_13 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(cfscore2, am_estimate2, use="complete.obs"), 2))

cors_14 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(cfscore2, js_ideology_contemporaneous2, use="complete.obs"), 2))

cors_15 <- plyr::ddply(bonica_2000s_grouped,c("party2"),  summarise, cor = round(cor(am_estimate2, js_ideology_contemporaneous2, use="complete.obs"), 2))

cors_dems1<-rbind(1,cors_1[1,2], cors_2[1,2], cors_3[1,2], cors_4[1,2], cors_5[1,2])
cors_dems2<-rbind(NA,1, cors_6[1,2], cors_7[1,2], cors_8[1,2], cors_9[1,2])
cors_dems3<-rbind(NA,NA,1, cors_10[1,2], cors_11[1,2], cors_12[1,2])
cors_dems4<-rbind(NA,NA,NA,1, cors_13[1,2], cors_14[1,2])
cors_dems5<-rbind(NA,NA,NA,NA,1, cors_15[1,2])
cors_dems6<-rbind(NA,NA,NA,NA,NA,1)

cors_dems<-cbind(cors_dems1, cors_dems2, cors_dems3, cors_dems4, cors_dems5, cors_dems6)

row.names(cors_dems)<-c("NPAT", "State Leg.", "Twitter", "CF-Score", "Survey", "Experts")


cors_reps1<-rbind(1,cors_1[2,2], cors_2[2,2], cors_3[2,2], cors_4[2,2], cors_5[2,2])
cors_reps2<-rbind(NA,1, cors_6[2,2], cors_7[2,2], cors_8[2,2], cors_9[2,2])
cors_reps3<-rbind(NA,NA,1, cors_10[2,2], cors_11[2,2], cors_12[2,2])
cors_reps4<-rbind(NA,NA,NA,1, cors_13[2,2], cors_14[2,2])
cors_reps5<-rbind(NA,NA,NA,NA,1, cors_15[2,2])
cors_reps6<-rbind(NA,NA,NA,NA,NA,1)

cors_reps<-cbind(cors_reps1, cors_reps2, cors_reps3, cors_reps4, cors_reps5, cors_reps6)
row.names(cors_reps)<-c("NPAT", "State Leg.", "Twitter", "CF-Score", "Survey", "Experts")

colnames(cors_dems)<-c("NPAT", "State Leg.", "Twitter", "CF-Score", "Survey", "Experts")
colnames(cors_reps)<-c("NPAT", "State Leg.", "Twitter", "CF-Score", "Survey", "Experts")

### Table 2
cat(print(xtable(cors_dems)),file="Table2.txt")

### Table 3
cat(print(xtable(cors_reps)),file="Table3.txt")

cors_dems[cors_dems==1]<-NA
mean(cors_dems, na.rm=T)
cors_reps[cors_reps==1]<-NA
mean(cors_reps, na.rm=T)


